package com.mcxx.util;

import cn.hutool.core.util.RandomUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

public class BatchNoUtil {


        public static String  getBatch(String busiCode,String areaCode){
            ReentrantLock lock = new ReentrantLock();
            StringBuffer  batchNo = new StringBuffer();
            lock.lock();
            batchNo.append(areaCode);
            batchNo.append(DateUtil.DateToString3(new Date()));
            batchNo.append(busiCode);
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            long millis = (System.currentTimeMillis() - c.getTimeInMillis());
            batchNo.append(millis);
            lock.unlock();
            return batchNo.toString();
        }

    public static String  getNewBatch(String sourceBatch){
        String randomInt = String.valueOf(RandomUtil.randomInt(1000));
        String newBatch = sourceBatch.substring(0,sourceBatch.length()-randomInt.length())+randomInt;
        if(newBatch.equals(sourceBatch)){
            return getNewBatch(sourceBatch);
        }else {
            return newBatch;
        }
    }

    public static void main(String[] args) {
        String batch = getBatch("110","123123");
        System.out.println(batch);
        System.out.println(getNewBatch(batch));
    }
}
